Add a way to delay snapshots in reftests
authorMatthias Clasen <mclasen@redhat.com>
Tue, 3 Jun 2014 02:31:49 +0000 (22:31 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 3 Jun 2014 02:35:59 +0000 (22:35 -0400)
This adds an inhibit api that code from the reftest module
can use to delay the taking of the snapshot. Also refactor
the code in gtk-reftest to use the inhibit mechanism for
its own delaying of the snapshot until after the first
expose.

testsuite/reftests/Makefile.am
testsuite/reftests/gtk-reftest.c
testsuite/reftests/gtk-reftest.h [new file with mode: 0644]

index ea623641dccc61454aa726d3068c7a161c9d059a..e7e61a5e9bab9e27e69b79d83bea314fe1f9bcc8 100644 (file)
@@ -28,7 +28,8 @@ gtk_reftest_LDADD = \
 gtk_reftest_SOURCES = \
        reftest-module.c                \
        reftest-module.h                \
-       gtk-reftest.c
+       gtk-reftest.c                   \
+       gtk-reftest.h
 
 clean-local:
        rm -rf output/ || true
index 74438c12a7c12f89278e28b9a071bc713f9fd6a6..d4323346be52b8f5e87b6530549b98eeabda2872 100644 (file)
@@ -237,12 +237,31 @@ quit_when_idle (gpointer loop)
   return G_SOURCE_REMOVE;
 }
 
+static gint inhibit_count;
+static GMainLoop *loop;
+
+void
+reftest_inhibit_snapshot (void)
+{
+  inhibit_count++;
+}
+
+void
+reftest_uninhibit_snapshot (void)
+{
+  g_assert (inhibit_count > 0);
+  inhibit_count--;
+
+  if (inhibit_count == 0)
+    g_idle_add (quit_when_idle, loop);
+}
+
 static void
-check_for_draw (GdkEvent *event, gpointer loop)
+check_for_draw (GdkEvent *event, gpointer data)
 {
   if (event->type == GDK_EXPOSE)
     {
-      g_idle_add (quit_when_idle, loop);
+      reftest_uninhibit_snapshot ();
       gdk_event_handler_set ((GdkEventFunc) gtk_main_do_event, NULL, NULL);
     }
 
@@ -254,18 +273,23 @@ snapshot_widget (GtkWidget *widget, SnapshotMode mode)
 {
   cairo_surface_t *surface;
   cairo_pattern_t *bg;
-  GMainLoop *loop;
   cairo_t *cr;
 
   g_assert (gtk_widget_get_realized (widget));
 
   loop = g_main_loop_new (NULL, FALSE);
+
   /* We wait until the widget is drawn for the first time.
    * We can not wait for a GtkWidget::draw event, because that might not
    * happen if the window is fully obscured by windowed child widgets.
    * Alternatively, we could wait for an expose event on widget's window.
-   * Both of these are rather hairy, not sure what's best. */
-  gdk_event_handler_set (check_for_draw, loop, NULL);
+   * Both of these are rather hairy, not sure what's best.
+   *
+   * We also use an inhibit mechanism, to give module functions a chance
+   * to delay the snapshot.
+   */
+  reftest_inhibit_snapshot ();
+  gdk_event_handler_set (check_for_draw, NULL, NULL);
   g_main_loop_run (loop);
 
   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
diff --git a/testsuite/reftests/gtk-reftest.h b/testsuite/reftests/gtk-reftest.h
new file mode 100644 (file)
index 0000000..aa527b6
--- /dev/null
@@ -0,0 +1,28 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2014 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GTK_REFTEST_H__
+#define __GTK_REFTEST_H__
+
+G_BEGIN_DECLS
+
+void reftest_inhibit_snapshot   (void);
+void reftest_uninhibit_snapshot (void);
+
+G_END_DECLS
+
+#endif /* __GTK_REFTEST_H__ */